package com.milk.milkmedia.ui;

import com.j256.ormlite.dao.Dao;
import com.milk.milkmedia.R;
import com.milk.milkmedia.api.BaseActivity;
import com.milk.milkmedia.dao.DBManager;
import com.milk.milkmedia.dao.UserInfoDB;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.sql.SQLException;

public class PasswordActivitty extends BaseActivity implements
        OnClickListener {

    private EditText mPassWordEdit;
    private EditText mPswAgainEdit;
    private Button mRegisterBtn;
    private String mPswStr, mPswAgainStr;
    private String mUserPhone;
    private Dao<UserInfoDB,String> mUserInfoDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_password);
        initView();
        getPhoneNum();

    }
    private void getPhoneNum() {
        Intent intent = this.getIntent();
        Bundle bundle = new Bundle();
        bundle =  intent.getExtras();
        mUserPhone = (String) bundle.get("user_phone");
        try {
            mUserInfoDao = DBManager.getInstance().getUserInfoDBHelper().getDao(UserInfoDB.class);
        } catch (SQLException e) {
            e.printStackTrace();
            Log.d(HomePageActivity.Tag,"创建mUserInfoDao失败");
        }
    }

    private void initView() {
        mPassWordEdit = (EditText) findViewById(R.id.password);
        mPswAgainEdit = (EditText) findViewById(R.id.isPassword);
        mRegisterBtn = (Button) findViewById(R.id.register);
        mRegisterBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId())
        {
            case R.id.register:
                mPswStr = mPassWordEdit.getText().toString();
                mPswAgainStr = mPswAgainEdit.getText().toString();
                boolean t = Judge(mPswStr, mPswAgainStr);
                if (Judge(mPswStr, mPswAgainStr)) {
                    UserInfoDB userInfoDB = new UserInfoDB();
                    userInfoDB.setmUserPhone(mUserPhone);
                    userInfoDB.setmPassword(mPswStr);
//                    intent = new Intent(this,HomePageActivity.class);
//                    startActivity(intent);
                }
                break;
            default:
                break;
        }
    }

    public boolean Judge(String mPswStr, String mPswAgainStr) {
        if (mPswStr.length() < 6) {
            Toast.makeText(PasswordActivitty.this, "密码不符合要求",
                    Toast.LENGTH_SHORT).show();
            return false;
        } else {
            if (mPswAgainStr.length() < 6) {
                Toast.makeText(PasswordActivitty.this, "确认密码不符合要求",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
            int i = Integer.parseInt(mPswStr);
            int j = Integer.parseInt(mPswAgainStr);
            if (i == j) {
                return true;
            } else {
                Toast.makeText(PasswordActivitty.this, "两次密码输入不一致",
                        Toast.LENGTH_SHORT).show();
                return false;
            }
        }
    }

}
